package sdu.icat.forum.service;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import sdu.icat.forum.repository.GroupMemberRepository;
import sdu.icat.forum.repository.GroupRepository;
import sdu.icat.forum.repository.GroupRequestRepository;
import sdu.icat.forum.smodel.Group;
import sdu.icat.forum.smodel.GroupMember;
import sdu.icat.forum.smodel.GroupRequest;
import sdu.icat.pj.base.ResponseMap;
import sdu.icat.pj.util.WebUtil;

@Service(value="group_service")
public class GroupService {

    private final Logger logger = LogManager.getLogger();
    
    @Autowired private GroupRepository groupRepository;
    
    @Autowired private GroupMemberRepository groupMemberRepository;
    
    @Autowired private GroupRequestRepository groupRequestRepository;
    
    /**
     * 创建团队
     * @param group
     * @return
     */
    public Group createGroup(Group group) {
	return groupRepository.save(group);
    }
    
    /**
     * 请求加入团队
     * @param group
     * @return
     */
    @Transactional
    public ResponseMap requestJoin(Group group,String context) {
	ResponseMap result = ResponseMap.newInstance();
	if(group.getStatus()!=-1) {
	    //获取自己的id
	    Long userid = WebUtil.getCurrentUser();
	    if(userid == null) {
		result.setFailedNoLoginMap("请登录", null);
		logger.info("请登录后再操作");
	    } else {
		GroupRequest request = new GroupRequest();
		request.setUserid(userid);
		request.setId(group.getId());
		request.setRequestContext(context);
		groupRequestRepository.save(request);
		inviteMember(userid,group.getId(),1);
	    }
	    
	} else {
	    result.setFailedMap("当前团队不允许任何人加入！", null);
	    logger.info("当前团队不允许任何人加入！");
	}
	return result;
    }
    
    /**
     * 提交申请
     * @param request
     * @return
     */
    @Transactional
    public boolean commitRequest(long requestid) {
	GroupRequest request = groupRequestRepository.findOne(requestid);
	GroupMember member = new GroupMember();
	if(request.getGroupid()==null) {
	    return false;
	}
	member.setGroupid(request.getGroupid());
	if(request.getUserid()==null) {
	    return false;
	}
	member.setUserid(request.getUserid());
	member.setLevel(3);
	groupMemberRepository.save(member);
	return true;
    }
    
    /**
     * 管理者邀请成员
     * @param userid
     * @param groupid
     * @return
     */
    @Transactional
    public boolean inviteMember(long userid,long groupid,int level) {
	GroupMember member = new GroupMember();
	member.setUserid(userid);
	member.setGroupid(groupid);
	member.setLevel(level);
	groupMemberRepository.save(member);
	return true;
    }
    
    @Transactional
    public List<Group> listMyGroup() {
	Long userid = WebUtil.getCurrentUser();
	List<GroupMember> gmembers = groupMemberRepository.findByUserid(userid);
	List<Group> groups = new ArrayList<>();
	for(GroupMember member:gmembers) {
	    Group g = groupRepository.findOne(member.getGroupid());
	    groups.add(g);
	}
	return groups;
    }
    
    public Group getOneGroup(Long id) {
	return groupRepository.findOne(id);
    }
}
